package com.infodms.dms.dao.claim.oldPart;

import java.math.BigDecimal;
import java.sql.ResultSet;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.infodms.dms.bean.ClaimApproveAndStoredReturnInfoBean;
import com.infodms.dms.bean.ClaimOldPartApproveStoreListBean;
import com.infodms.dms.bean.TtAsWrOldPartSignDetailListBean;
import com.infodms.dms.common.Constant;
import com.infodms.dms.dao.common.BaseDao;
import com.infodms.dms.po.TmPtPartBasePO;
import com.infodms.dms.po.TmPtSupplierPO;
import com.infodms.dms.po.TtAsWrOldReturnedDetailPO;
import com.infodms.dms.po.TtAsWrOldReturnedPO;
import com.infodms.dms.po.TtAsWrPartStockPO;
import com.infodms.dms.util.CommonUtils;
import com.infodms.dms.util.sequenceUitl.SequenceManager;
import com.infoservice.po3.bean.PO;
import com.infoservice.po3.bean.PageResult;
/**
 * 类说明：索赔旧件管理--索赔旧件入库Dao层
 * 作者：  赵伦达
 */
@SuppressWarnings("unchecked")
public class ClaimApproveOldPartStoredDao extends BaseDao{
	
	public static Logger logger = Logger.getLogger(ClaimApproveOldPartStoredDao.class);
	
	private static final ClaimApproveOldPartStoredDao dao = null;
	
	public static final ClaimApproveOldPartStoredDao getInstance() {
	   if(dao==null) return new ClaimApproveOldPartStoredDao();
	   return dao;
	}
	/**
	 * Function：索赔旧件审批入库--按页面条件查询回运清单信息
	 * @param  ：	
	 * @return:		@param params
	 * @return:		@param curPage
	 * @return:		@param pageSize
	 * @return:		@return 
	 * @throw：	
	 * LastUpdate：	2010-6-13
	 */
	public PageResult<ClaimOldPartApproveStoreListBean> queryClaimBackList(Map params,int curPage, int pageSize){
		//获取前台条件
		String company_id=ClaimTools.dealParamStr(params.get("company_id"));//登陆人所在companyId
		String dealer_code=ClaimTools.dealParamStr(params.get("i_dealer_code"));//经销商代码
		String dealer_name=ClaimTools.dealParamStr(params.get("i_dealer_name"));//经销商简称
		String return_no=ClaimTools.dealParamStr(params.get("i_return_no"));//回运清单号
		String in_start_date=ClaimTools.dealParamStr(params.get("i_create_start_date"));//入库开始日期
		String in_end_date=ClaimTools.dealParamStr(params.get("i_create_end_date"));//入库结束日期
		String return_start_date=ClaimTools.dealParamStr(params.get("i_submit_start_date"));//提报开始日期
		String return_end_date=ClaimTools.dealParamStr(params.get("i_submit_end_date"));//提报结束日期
		String transport_type=ClaimTools.dealParamStr(params.get("i_transport_type"));//货运方式
		String back_type=ClaimTools.dealParamStr(params.get("i_back_type"));//回运单状态
		String trans_no=ClaimTools.dealParamStr(params.get("i_trans_no"));//货运单号
		String yieldlys=ClaimTools.dealParamStr(params.get("yieldlys"));//用户拥有的产地权限
		String yieldly=ClaimTools.dealParamStr(params.get("yieldly"));//查询条件--产地
		
		StringBuffer sqlStr=new StringBuffer();
		StringBuffer whereStr=new StringBuffer();
		StringBuffer groupStr=new StringBuffer();
		StringBuffer orderStr=new StringBuffer();
		
		sqlStr.append("select tawor.id,td.dealer_code dealer_id,td.dealer_shortname dealer_name,tawor.tel,\n" );
		sqlStr.append("tawor.return_no claim_no,to_char(tawor.in_warhouse_date,'YYYY-MM-DD') create_date,\n" );
		//sqlStr.append("to_char(tawor.in_warhouse_date,'YYYY-MM-DD') in_warhouse_date,");
		sqlStr.append("to_char(tawor.return_date,'YYYY-MM-DD') return_date,\n" );
		sqlStr.append("tawor.return_type,tc.code_desc return_desc,\n" );
		sqlStr.append("nvl(sum(tawor.wr_amount),0) wr_amount,\n" );
		sqlStr.append("nvl(sum(tawor.parkage_amount),0) parkage_amount,\n" );
		sqlStr.append("nvl(sum(tawor.part_amount),0) part_amount,tawor.status back_type,tcc.code_desc back_desc,tawor.tran_no trans_no\n" );
		sqlStr.append("from tt_as_wr_old_returned tawor,tm_dealer td,tc_code tc,tc_code tcc\n" );
		sqlStr.append("where tawor.dealer_id=td.dealer_id and tawor.transport_type=tc.code_id\n" );
		/**********2010-12-14**********/
		//sqlStr.append("and tawor.status not in('"+Constant.BACK_LIST_STATUS_04+"')\n" );
		/********************/
		sqlStr.append("and tawor.status=tcc.code_id\n" );
		sqlStr.append("and tawor.oem_company_id="+company_id+"\n" );
		sqlStr.append("and tawor.yieldly in ("+yieldlys+")\n" );
		//条件
		if(dealer_code!=null&&!"".equals(dealer_code)){
			String[] temp=dealer_code.split(",");
			String str="";
			if(temp.length>0){
				sqlStr.append(" and (");
				for(int count=0;count<temp.length;count++){
					str+=" td.dealer_code='"+temp[count].replaceAll("'", "\''")+"' or\n";
				}
				sqlStr.append(str.substring(0, str.length()-3));
				sqlStr.append(")\n");
			}
		}
		if(yieldly!=null&&!"".equals(yieldly))
			whereStr.append(" and tawor.yieldly = '"+yieldly+"'\n");
		if(dealer_name!=null&&!"".equals(dealer_name))
			whereStr.append(" and td.dealer_shortname like '%"+dealer_name.replaceAll("'", "\''")+"%'\n");
		if(return_no!=null&&!"".equals(return_no))
			whereStr.append(" and tawor.return_no like '%"+return_no.toUpperCase().replaceAll("'", "\''")+"%'\n");
		if(in_start_date!=null&&!"".equals(in_start_date))
			whereStr.append(" and tawor.in_warhouse_date>=to_date('" + in_start_date+ " 00:00:00','YYYY-MM-DD HH24:mi:ss')\n");
		if(in_end_date!=null&&!"".equals(in_end_date))
			whereStr.append(" and tawor.in_warhouse_date<=to_date('" + in_end_date+ " 23:59:59','YYYY-MM-DD HH24:mi:ss')\n");
		if(return_start_date!=null&&!"".equals(return_start_date))
			whereStr.append(" and tawor.return_date>=to_date('" + return_start_date+ " 00:00:00','YYYY-MM-DD HH24:mi:ss')\n");
		if(return_end_date!=null&&!"".equals(return_end_date))
			whereStr.append(" and tawor.return_date<=to_date('" + return_end_date+ " 23:59:59','YYYY-MM-DD HH24:mi:ss')\n");
		if(transport_type!=null&&!"".equals(transport_type))
			whereStr.append(" and tawor.transport_type="+transport_type+"\n");
		if(back_type!=null&&!"".equals(back_type)){
			whereStr.append(" and tawor.status="+back_type+"\n");
		}else{
			whereStr.append(" and (tawor.status="+Constant.BACK_LIST_STATUS_02+" or tawor.status="+Constant.BACK_LIST_STATUS_03+" or tawor.status="+Constant.BACK_LIST_STATUS_04+")\n");
		}
		if(trans_no!=null&&!"".equals(trans_no))
			whereStr.append(" and tawor.tran_no like '%"+trans_no.toUpperCase().replaceAll("'", "\''")+"%'\n");
		
		groupStr.append("group by tawor.id,td.dealer_code,td.dealer_shortname,tawor.return_no,tawor.in_warhouse_date,tawor.tel,\n" );
		groupStr.append("tawor.create_date,tawor.return_date,tawor.return_type,tc.code_desc,tawor.status,tcc.code_desc,tawor.tran_no\n");
		
		orderStr.append(" order by tawor.id desc\n");

		PageResult<ClaimOldPartApproveStoreListBean> pr = pageQuery(ClaimOldPartApproveStoreListBean.class,
				sqlStr.toString()+whereStr.toString()+groupStr.toString()+orderStr.toString(),null, pageSize, curPage);
		return pr;
	}
	/**
	 * Function：通过回运主键查询回运信息
	 * @param  ：	
	 * @return:		@return 
	 * @throw：	
	 * LastUpdate：	2010-6-13 赵伦达
	 */
	public ClaimApproveAndStoredReturnInfoBean getApproveAndStoredReturnInfo(Map params){
		String claim_id=ClaimTools.dealParamStr(params.get("i_claim_id"));
		if(!"".equals(claim_id)){
			//
			StringBuffer sqlStr= new StringBuffer();
			sqlStr.append("select tawor.id,tm.dealer_code,tm.dealer_name,tawor.tel,\n" );
			sqlStr.append("torg.org_name attach_area,tawor.transport_type,tc.code_desc transport_desc,\n" );
			sqlStr.append("sum(tawor.parkage_amount) parkage_amount,tawor.return_no,\n" );
			sqlStr.append("to_char(tawor.create_date,'YYYY-MM-DD') create_date,\n" );
			sqlStr.append("to_char(tawor.return_date,'YYYY-MM-DD') return_date,\n" );
			sqlStr.append("sum(tawor.wr_amount) wr_amount,sum(tawor.part_item_amount) part_item_amount,\n" );
			sqlStr.append("sum(tawor.part_amount) part_amount,tawor.return_type,\n" );
			sqlStr.append("tcc.code_desc return_desc,tawor.wr_start_date,tawor.tran_no tran_no\n" );
			sqlStr.append("from tt_as_wr_old_returned tawor,tm_dealer tm,tm_dealer_org_relation tdor,\n" );
			sqlStr.append("tm_org torg,tc_code tc,tc_code tcc\n" );
			sqlStr.append("where tawor.transport_type=tc.code_id and tawor.return_type=tcc.code_id\n" );
			sqlStr.append("and tawor.dealer_id=tm.dealer_id and f_get_pid(tawor.dealer_id)=tdor.dealer_id\n" );
			sqlStr.append("and tdor.org_id=torg.org_id\n" );
			sqlStr.append("and tawor.id="+claim_id+"\n" );
			sqlStr.append("group by tawor.id,tawor.dealer_id,tm.dealer_code,tm.dealer_name,torg.org_name,tawor.tel,\n" );
			sqlStr.append("tawor.transport_type,tc.code_desc,tawor.return_no,tawor.create_date,\n" );
			sqlStr.append("tawor.return_date,tawor.return_type,tcc.code_desc,tawor.wr_start_date,tawor.tran_no");
			System.out.println("sqlsql=="+sqlStr);
			PageResult<ClaimApproveAndStoredReturnInfoBean> pr = pageQuery(ClaimApproveAndStoredReturnInfoBean.class,sqlStr.toString(), null,10,1);
			
			if(pr!=null&&pr.getTotalPages()>0){
				return (ClaimApproveAndStoredReturnInfoBean)pr.getRecords().get(0);
			}else{
				return null;
			}
		}else{
			return null;
		}
	}
	public PageResult<TtAsWrOldPartSignDetailListBean> queryClaimBackDetailList(Map params,int curPage, int pageSize){
		String claim_id=ClaimTools.dealParamStr(params.get("i_claim_id"));
		if(!"".equals(claim_id)){
			StringBuffer sqlStr= new StringBuffer();
			sqlStr.append("select taword.id,taword.claim_no,taword.vin,\n" );
			sqlStr.append("tawp.part_code,tawp.part_name,nvl(sum(taword.return_amount),0) return_amount,\n" );
			sqlStr.append("nvl(sum(taword.sign_amount),0) sign_amount,\n" );
			sqlStr.append("taword.box_no,taword.warehouse_region,taword.deduct_remark,nvl(tc.code_desc,'') deduct_desc\n" );
			sqlStr.append("from tt_as_wr_old_returned tawor,\n" );
			sqlStr.append("tt_as_wr_old_returned_detail taword,\n" );
			sqlStr.append("tt_as_wr_partsitem tawp,tc_code tc\n" );
			sqlStr.append("where tawor.id=taword.return_id and taword.part_id=tawp.part_id and taword.deduct_remark=tc.code_id(+)\n" );
			sqlStr.append("and tawor.id="+claim_id+"\n" );
			sqlStr.append("group by taword.id,taword.claim_no,taword.vin,\n" );
			sqlStr.append("tawp.part_code,tawp.part_name,tc.code_desc,\n" );
			sqlStr.append("taword.box_no,taword.warehouse_region,taword.deduct_remark,taword.create_date\n");
			sqlStr.append("order by taword.create_date\n");
			
			PageResult<TtAsWrOldPartSignDetailListBean> pr = pageQuery(TtAsWrOldPartSignDetailListBean.class,sqlStr.toString(),null, pageSize, curPage);
			if(pr!=null){
				return pr;
			}else{
				return null;
			}
		}else{
			return null;
		}
		
	}
	
	public List<TtAsWrOldPartSignDetailListBean> queryClaimBackDetailList3(Map params){
		String claim_id=ClaimTools.dealParamStr(params.get("i_claim_id"));
		if(!"".equals(claim_id)){
			StringBuffer sqlStr= new StringBuffer();
			sqlStr.append("select taword.id,taword.claim_no,taword.vin,\n" );
			sqlStr.append("tawp.part_code,tawp.part_name,nvl(sum(taword.return_amount),0) return_amount,\n" );
			sqlStr.append("nvl(sum(taword.sign_amount),0) sign_amount,\n" );
			sqlStr.append("taword.box_no,taword.warehouse_region,taword.deduct_remark,nvl(tc.code_desc,'') deduct_desc\n" );
			sqlStr.append("from tt_as_wr_old_returned tawor,\n" );
			sqlStr.append("tt_as_wr_old_returned_detail taword,\n" );
			sqlStr.append("tt_as_wr_partsitem tawp,tc_code tc\n" );
			sqlStr.append("where tawor.id=taword.return_id and taword.part_id=tawp.part_id and taword.deduct_remark=tc.code_id(+)\n" );
			sqlStr.append("and tawor.id="+claim_id+"\n" );
			sqlStr.append("group by taword.id,taword.claim_no,taword.vin,\n" );
			sqlStr.append("tawp.part_code,tawp.part_name,tc.code_desc,\n" );
			sqlStr.append("taword.box_no,taword.warehouse_region,taword.deduct_remark,taword.create_date\n");
			sqlStr.append("order by taword.create_date\n");
			
			List<TtAsWrOldPartSignDetailListBean> pr = this.select(TtAsWrOldPartSignDetailListBean.class,sqlStr.toString(),null);
			if(pr!=null){
				return pr;
			}else{
				return null;
			}
		}else{
			return null;
		}
		
	}
	
	public List<Map<String,Object>> queryClaimBackDetailList2(Map params){
		String claim_id=ClaimTools.dealParamStr(params.get("i_claim_id"));
		String boxNo=ClaimTools.dealParamStr(params.get("boxNo"));
		if(!"".equals(claim_id)){
			StringBuffer sqlStr= new StringBuffer();
			sqlStr.append("select * from (select taword.id,taword.claim_no,taword.vin,c.id as claim_id,taword.PRODUCER_NAME,to_char(c.RO_STARTDATE,'yyyy-mm-dd') as RO_STARTDATE,c.REMARK,del.dealer_code,\n" );
			sqlStr.append("tawp.part_code,tawp.part_name,nvl(sum(taword.return_amount),0) return_amount,\n" );
			sqlStr.append("nvl(sum(taword.sign_amount),0) sign_amount,\n" );
			sqlStr.append("taword.box_no,taword.warehouse_region,taword.deduct_remark,nvl(tc.code_desc,'') deduct_desc\n" );
			sqlStr.append("from tt_as_wr_old_returned tawor,\n" );
			sqlStr.append("tt_as_wr_old_returned_detail taword,\n" );
			sqlStr.append("tt_as_wr_partsitem tawp,tc_code tc,TT_AS_WR_APPLICATION c,tm_dealer del\n" );
			sqlStr.append("where tawor.id=taword.return_id and taword.part_id=tawp.part_id and taword.deduct_remark=tc.code_id(+)\n" );
			sqlStr.append("and taword.is_sign=0 and tawor.id="+claim_id+" and c.claim_no=taword.claim_no and del.dealer_id=c.dealer_id\n" );
			if(!"".equals(boxNo)){
				sqlStr.append("and taword.box_no='"+boxNo+"'\n" );
			}
			sqlStr.append("group by taword.id,taword.claim_no,taword.vin,c.id,taword.PRODUCER_NAME,c.RO_STARTDATE,c.REMARK,del.dealer_code,\n" );
			sqlStr.append("tawp.part_code,tawp.part_name,tc.code_desc,\n" );
			sqlStr.append("taword.box_no,taword.warehouse_region,taword.deduct_remark,taword.create_date\n");
			//sqlStr.append("order by length(taword.box_no) asc,taword.box_no,nlssort(tawp.part_CODE,'NLS_SORT=SCHINESE_RADICAL_M') asc\n");
			
			sqlStr.append("union all\n" );
			
			sqlStr.append("select taword.id,taword.claim_no,taword.vin,c.id as claim_id,taword.PRODUCER_NAME,to_char(c.RO_STARTDATE,'yyyy-mm-dd') as RO_STARTDATE,c.REMARK,del.dealer_code,\n" );
			sqlStr.append("tawp.part_code,tawp.part_name,nvl(sum(taword.return_amount),0) return_amount,\n" );
			sqlStr.append("nvl(sum(taword.sign_amount),0) sign_amount,\n" );
			sqlStr.append("taword.box_no,taword.warehouse_region,taword.deduct_remark,nvl(tc.code_desc,'') deduct_desc\n" );
			sqlStr.append("from tt_as_wr_old_returned tawor,\n" );
			sqlStr.append("tt_as_wr_old_returned_detail taword,\n" );
			sqlStr.append("tt_as_wr_partsitem tawp,tc_code tc,tt_as_wr_application_backup c,tm_dealer del\n" );
			sqlStr.append("where tawor.id=taword.return_id and taword.part_id=tawp.part_id and taword.deduct_remark=tc.code_id(+)\n" );
			sqlStr.append("and taword.is_sign=0 and tawor.id="+claim_id+" and c.claim_no=taword.claim_no and del.dealer_id=c.dealer_id\n" );
			if(!"".equals(boxNo)){
				sqlStr.append("and taword.box_no='"+boxNo+"'\n" );
			}
			sqlStr.append("group by taword.id,taword.claim_no,taword.vin,c.id,taword.PRODUCER_NAME,c.RO_STARTDATE,c.REMARK,del.dealer_code,\n" );
			sqlStr.append("tawp.part_code,tawp.part_name,tc.code_desc,\n" );
			sqlStr.append("taword.box_no,taword.warehouse_region,taword.deduct_remark,taword.create_date)\n");
			//sqlStr.append("order by length(taword.box_no) asc,taword.box_no,nlssort(tawp.part_CODE,'NLS_SORT=SCHINESE_RADICAL_M') asc\n");
			sqlStr.append("order by length(box_no) asc,box_no,nlssort(part_CODE,'NLS_SORT=SCHINESE_RADICAL_M') asc\n");
			
			System.out.println("sql==="+sqlStr);
			List<Map<String,Object>> pr = this.pageQuery(sqlStr.toString(), null, this.getFunName());
			if(pr!=null){
				return pr;
			}else{
				return null;
			}
		}else{
			return null;
		}
	}
	
	public List<Map<String,Object>> queryClaimBackDetailList22(Map params){
		String claim_id=ClaimTools.dealParamStr(params.get("id"));
		String boxNo=ClaimTools.dealParamStr(params.get("boxNo"));
		if(!"".equals(claim_id)){
			StringBuffer sqlStr= new StringBuffer();
			sqlStr.append("select taword.id,taword.claim_no,taword.vin,c.id as claim_id,taword.PRODUCER_NAME,to_char(c.RO_STARTDATE,'yyyy-mm-dd') as RO_STARTDATE,c.REMARK,del.dealer_code,\n" );
			sqlStr.append("tawp.part_code,tawp.part_name,nvl(sum(taword.return_amount),0) return_amount,\n" );
			sqlStr.append("nvl(sum(taword.sign_amount),0) sign_amount,\n" );
			sqlStr.append("taword.box_no,taword.warehouse_region,taword.deduct_remark,nvl(tc.code_desc,'') deduct_desc\n" );
			sqlStr.append("from tt_as_wr_old_returned tawor,\n" );
			sqlStr.append("tt_as_wr_old_returned_detail taword,\n" );
			sqlStr.append("tt_as_wr_partsitem tawp,tc_code tc,TT_AS_WR_APPLICATION c,tm_dealer del\n" );
			sqlStr.append("where tawor.id=taword.return_id and taword.part_id=tawp.part_id and taword.deduct_remark=tc.code_id(+)\n" );
			sqlStr.append("and tawor.id="+claim_id+" and c.claim_no=taword.claim_no and del.dealer_id=c.dealer_id\n" );
			if(!"".equals(boxNo)){
				sqlStr.append("and taword.box_no='"+boxNo+"'\n" );
			}
			sqlStr.append("group by taword.id,taword.claim_no,taword.vin,c.id,taword.PRODUCER_NAME,c.RO_STARTDATE,c.REMARK,del.dealer_code,\n" );
			sqlStr.append("tawp.part_code,tawp.part_name,tc.code_desc,\n" );
			sqlStr.append("taword.box_no,taword.warehouse_region,taword.deduct_remark,taword.create_date\n");
			sqlStr.append("order by taword.box_no, tawp.part_name asc\n");
			System.out.println("sql==="+sqlStr);
			List<Map<String,Object>> pr = this.pageQuery(sqlStr.toString(), null, this.getFunName());
			if(pr!=null){
				return pr;
			}else{
				return null;
			}
		}else{
			return null;
		}
	}
	
	/*********Iverson add By 2010-11-18 查询出旧件起止时间******************/
	public List<Map<String,Object>> queryClaimBackDetailList3(String claim_id){
		if(!"".equals(claim_id)){
			StringBuffer sqlStr= new StringBuffer();	
			sqlStr.append("SELECT OO.WR_START_DATE FROM TT_AS_WR_RETURNED_ORDER OO WHERE OO.ID IN (\n" );
			sqlStr.append("select DISTINCT OD.DEALER_RETURN_ID from tt_as_wr_old_returned_detail OD WHERE OD.RETURN_ID='"+claim_id+"')" );
			List<Map<String,Object>> pr = this.pageQuery(sqlStr.toString(), null, this.getFunName());
			if(pr!=null){
				return pr;
			}else{
				return null;
			}
		}else{
			return null;
		}
	}
	
	/**
	 * Function：签收回运单，修改回运明细表
	 * @param  ：	
	 * @return:		@return 
	 * @throw：	
	 * LastUpdate：	2010-6-13
	 */
	public int SignBackOldPartOper(Map params){
		String idStr=ClaimTools.dealParamStr(params.get("idStr"));
		String return_ord_id=ClaimTools.dealParamStr(params.get("return_ord_id"));
		String logonId=ClaimTools.dealParamStr(params.get("logonId"));
		String[] strArr=idStr.split(",");
		int updateNum=0;
		for(int count=0;count<strArr.length;count++){
			String signNum=ClaimTools.dealParamStr(params.get("signNum"+strArr[count]));//签收数
			String wrHouseNo=ClaimTools.dealParamStr(params.get("wrHouse"+strArr[count]));//库区
			String deduct=ClaimTools.dealParamStr(params.get("deduct"+strArr[count]));//抵扣原因
			
			TtAsWrOldReturnedDetailPO idObj=new TtAsWrOldReturnedDetailPO();
			idObj.setId(Long.parseLong(strArr[count]));
			
			TtAsWrOldReturnedDetailPO vo=new TtAsWrOldReturnedDetailPO();
			vo.setSignAmount(Integer.parseInt(signNum));
			vo.setWarehouseRegion(wrHouseNo);
			if(!"".equals(deduct)){
				vo.setDeductRemark(Integer.parseInt(deduct));
			}else{
				vo.setDeductRemark(0);
			}
			
			updateNum+=update(idObj,vo);
		}
		/**
		 *根据需求增加部分签收功能 modify by zhaolunda 2010-08-06 
		 */
		TtAsWrOldReturnedPO idObj=new TtAsWrOldReturnedPO();
		idObj.setId(Long.parseLong(return_ord_id));
		
		TtAsWrOldReturnedPO vo=new TtAsWrOldReturnedPO();
		vo.setStatus(Constant.BACK_LIST_STATUS_03);//部分签收
		vo.setUpdateBy(Long.parseLong(logonId));
		vo.setUpdateDate(new Date());
		update(idObj,vo);
		
		return updateNum;
	}
	/**
	 * Function：回运清单--签收完成操作
	 * @param  ：	
	 * @return:		@param params
	 * @return:		@return 
	 * @throw：	
	 * LastUpdate：	2010-6-17
	 * 2010-10-13 修改 库存中加入产地限制
	 * 描述：
	 *     1、检测库存中是否存在该配件时，加入产地限制
	 *     2、记录库存记录是加入产地信息
	 */
	public int SignFinishOper(Map params){
		String id=ClaimTools.dealParamStr(params.get("id"));
		//String transNo=ClaimTools.dealParamStr(params.get("transNo"));
		String logonId=ClaimTools.dealParamStr(params.get("logonId"));
		String company_id=ClaimTools.dealParamStr(params.get("company_id"));//登陆人所在companyId
		int updateNum=0;
		
		//更改索赔回运清单表
		TtAsWrOldReturnedPO idObj=new TtAsWrOldReturnedPO();
		idObj.setId(Long.parseLong(id));
		
		TtAsWrOldReturnedPO vo=new TtAsWrOldReturnedPO();
		vo.setInWarhouseDate(new Date());//主表签收入库时间
		//vo.setTranNo(transNo);
		//vo.setStatus(Constant.BACK_LIST_STATUS_04);//已签收/////////////////////////Iverson 2010-12-14
		vo.setUpdateBy(Long.parseLong(logonId));
		vo.setUpdateDate(new Date());
		updateNum=update(idObj,vo);
		//更新旧件库存表
		StringBuffer sqlStr= new StringBuffer();
		sqlStr.append("select * from tt_as_wr_old_returned_detail where return_id="+id);
		//回运清单明细表中数据不止10条，不应加分页
	    //List list=pageQuery(TtAsWrOldReturnedDetailPO.class,sqlStr.toString(),null, 10, 1).getRecords();
		List list=this.select(TtAsWrOldReturnedDetailPO.class,sqlStr.toString(),null);
		if(list!=null&&list.size()>0){
			for(int count=0;count<list.size();count++){
				TtAsWrOldReturnedDetailPO detailInfo=new TtAsWrOldReturnedDetailPO();
				detailInfo=(TtAsWrOldReturnedDetailPO)list.get(count);
				//判断旧件库存表是否已存在配件信息
				PageResult<TtAsWrPartStockPO> pr=getPartStockData(company_id,detailInfo.getPartCode(),detailInfo.getProcFactory());
				
				if(pr!=null&&pr.getTotalRecords()>0){//存在只需要修改库存表配件数量
					TtAsWrPartStockPO dataObj=(TtAsWrPartStockPO)pr.getRecords().get(0);
					TtAsWrPartStockPO updateObj=new TtAsWrPartStockPO();
					updateObj.setId(dataObj.getId());
					
					TtAsWrPartStockPO voPartStock=new TtAsWrPartStockPO();
					voPartStock.setReturnAmount(dataObj.getReturnAmount()+detailInfo.getSignAmount());//已存在的回运数量加上本次配件回运数量
					voPartStock.setUpdateBy(Long.parseLong(logonId));
					voPartStock.setUpdateDate(new Date());
					update(updateObj,voPartStock);
				}else{//不存在需要插入新的库存表信息
					TtAsWrPartStockPO insertPartStock=new TtAsWrPartStockPO();
					insertPartStock.setId(Long.parseLong(SequenceManager.getSequence("")));
					//获得供应商代码
					insertPartStock.setProducerId(getSupplyId(detailInfo.getProducerCode()));//供应商
					TmPtPartBasePO partBaseInfo=getPartInfo(String.valueOf(detailInfo.getPartId()),company_id);
					if(partBaseInfo!=null){
						insertPartStock.setPartId(partBaseInfo.getPartId());//配件id
						insertPartStock.setPartCode(partBaseInfo.getPartCode());//配件code
					}
					insertPartStock.setReturnAmount(detailInfo.getSignAmount());
					insertPartStock.setOutAmount(0);
					insertPartStock.setOemCompanyId(Long.parseLong(company_id));
					insertPartStock.setCreateBy(Long.parseLong(logonId));
					insertPartStock.setCreateDate(new Date());
					insertPartStock.setYieldly(Integer.parseInt(detailInfo.getProcFactory()));
					insert(insertPartStock);
				}
			}
		}
		
		
		return updateNum;
	}
	@Override
	protected PO wrapperPO(ResultSet rs, int idx) {
		return null;
	}

	/**
	 * 查看表中是否存在记录
	 * @param company_id 区分微车轿车
	 * @param partCode 配件代码
	 * @return
	 */
	public PageResult<TtAsWrPartStockPO> getPartStockData(String company_id,String partCode,String yieldly){
		
		if(yieldly==null || partCode==null)
			return null;
		
		StringBuffer sqlStr= new StringBuffer();
		sqlStr.append("select * from TT_AS_WR_PART_STOCK where oem_company_id="+company_id+"\n");
		sqlStr.append(" and part_code='" + CommonUtils.checkNull(partCode)+"'\n");
		sqlStr.append(" and YIELDLY=" + yieldly);
		/*
		sqlStr.append("select * from TT_AS_WR_PART_STOCK where oem_company_id="+company_id+" and part_code=(");
		sqlStr.append("select part_code from tt_as_wr_partsitem where part_id="+partId+")");
		*/
		PageResult<TtAsWrPartStockPO> pr = pageQuery(TtAsWrPartStockPO.class,sqlStr.toString(),null, 10, 1);
		
		if(pr!=null&&pr.getTotalPages()>0){
			return pr;
		}else{
			return null;
		}
	}
	/**
	 * Function：通过供应商code获得供应商id
	 * @param  ：	
	 * @return:		@param supplier_code
	 * @return:		@return 
	 * @throw：	
	 * LastUpdate：	2010-6-17
	 */
	public Long getSupplyId(String supplier_code){
		StringBuffer sqlStr= new StringBuffer();
		sqlStr.append("select supplier_id from TM_PT_SUPPLIER where supplier_code='"+supplier_code+"' and is_del=0");
		PageResult<TmPtSupplierPO> pr = pageQuery(TmPtSupplierPO.class,sqlStr.toString(),null, 10, 1);
		if(pr!=null&&pr.getTotalRecords()>0){
			return ((TmPtSupplierPO)pr.getRecords().get(0)).getSupplierId();
		}else{
			return 0l;
		}
	}
	/**
	 * Function：通过索赔配件表的主键id
	 *           获得配件信息表中的配件基本信息
	 * @param  ：	
	 * @return:		@param part_id
	 * @return:		@return 
	 * @throw：	
	 * LastUpdate：	2010-6-17
	 */
	public TmPtPartBasePO getPartInfo(String part_id,String company_id){
		StringBuffer sqlStr= new StringBuffer();
		sqlStr.append("select * from TM_PT_PART_BASE where part_code=(");
		sqlStr.append("select part_code from tt_as_wr_partsitem where part_id="+part_id+")and is_del=0 and oem_company_id="+company_id);
		PageResult<TmPtPartBasePO> pr = pageQuery(TmPtPartBasePO.class,sqlStr.toString(),null, 10, 1);
		if(pr!=null&&pr.getTotalRecords()>0){
			return (TmPtPartBasePO)pr.getRecords().get(0);
		}else{
			return null;
		}
	}
	/**
	 * Function：检查签收工作是否完成
	 * @param  ：	
	 * @return:		@param params
	 * @return:		@return 
	 * @throw：	
	 * LastUpdate：	2010-7-24 ZLD
	 */
	public String validateIsSignOper(Map params){
		String idStr=ClaimTools.dealParamStr(params.get("idStr"));//回运明细id
		StringBuffer sqlStr= new StringBuffer();
		
		if("".equals(idStr)){
			return "0";//不符合签收要求
		}
		String[] idArr=idStr.split(",");
		for(int count=0;count<idArr.length;count++){
			TtAsWrOldReturnedDetailPO chekObj=new TtAsWrOldReturnedDetailPO();
			sqlStr.delete(0, sqlStr.length());
			sqlStr.append("select * from tt_as_wr_old_returned_detail\n" );
			sqlStr.append("where id="+idArr[count]);
			List list=select(TtAsWrOldReturnedDetailPO.class, sqlStr.toString(), null);
			if(list!=null&&list.size()>0){
				chekObj=(TtAsWrOldReturnedDetailPO)list.get(0);
				Integer diff_numm=chekObj.getReturnAmount()-chekObj.getSignAmount();
				int dueduct_reason=chekObj.getDeductRemark().intValue();
				if(diff_numm>0&&dueduct_reason>0){//存在差异并且已在数据库中注明抵扣原因
					//return "1";//已完成签收操作
				}else if(diff_numm==0&&dueduct_reason==0){//不存在差异并且抵扣原因为空
					//return "1";//已完成签收操作
			    }else{
					return "0";//未完成
				}
			}else{
				return "0";
			}
		}
		return "1";
	}
	
	
	
	public List<Map<String,Object>> queryReturnOrder(String id){
		if(!"".equals(id)){
			StringBuffer sqlStr= new StringBuffer();	
			sqlStr.append("select u.dealer_id,u.yieldly ,u.wr_start_date,u.status,u.id from Tt_As_Wr_Returned_Order u  where u.id in(\n" );
			sqlStr.append("select distinct t.dealer_return_id from tt_as_wr_old_returned_detail t where t.return_id in(" );
			sqlStr.append("select w.id from tt_as_wr_old_returned  w where w.id='"+id+"'))" );
			List<Map<String,Object>> pr = this.pageQuery(sqlStr.toString(), null, this.getFunName());
			if(pr!=null){
				return pr;
			}else{
				return null;
			}
		}else{
			return null;
		}
	}
	
	public List<Map<String,Object>> queryMinDate(String id){
		if(!"".equals(id)){
			StringBuffer sqlStr= new StringBuffer();	
			sqlStr.append("select u.yieldly, min(u.wr_start_date) as wr_start_date,u.dealer_id from Tt_As_Wr_Returned_Order u  where u.id in(\n" );
			sqlStr.append("select distinct t.dealer_return_id from tt_as_wr_old_returned_detail t where t.return_id in(" );
			sqlStr.append("select w.id from tt_as_wr_old_returned  w where w.id='"+id+"'))" );
			sqlStr.append("group by u.wr_start_date,u.yieldly,u.dealer_id" );
			List<Map<String,Object>> pr = this.pageQuery(sqlStr.toString(), null, this.getFunName());
			if(pr!=null){
				return pr;
			}else{
				return null;
			}
		}else{
			return null;
		}
	}
	
	public int queryIsSign(long id){
		int count=0;
		if(!"".equals(id)){
			StringBuffer sqlStr= new StringBuffer();	
			sqlStr.append("select count(*) as count from tt_as_wr_old_returned_detail where is_sign=0 and return_id='"+id+"'\n" );
			List<Map<String,Object>> pr = this.pageQuery(sqlStr.toString(), null, this.getFunName());
			count=((BigDecimal)pr.get(0).get("COUNT")).intValue();
		}
		return count;
	}
}
